package com.itheima.controller;

import com.itheima.pojo.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author wuJiaWei
 * @version 1.0
 */
@ComponentScan
@RestController
public class RequestController {
    /*********************************************************简单参数**********************************************************/
    //原始方式
    @RequestMapping("/simpleParam")
    public String simpleParam(HttpServletRequest request){
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        int ageInt = Integer.parseInt(age);
        return "name:"+name+" age:"+ageInt;
    }
    //SpringBoot方式
    @RequestMapping("/simpleParam2")
    public String simpleParam2(String name,int age){//形参名和请求参数名保持一致
        return "name:"+name+" age:"+age;
    }

    //@RequestParam注解映射
    @RequestMapping("/simpleParam3")
    public String simpleParam3(@RequestParam(name = "username",required = false) String name, int age){//形参名和请求参数名保持一致
        return "name:"+name+" age:"+age;
    }

    /*********************************************************实体参数**********************************************************/
    //实体参数：简单实体对象
    @RequestMapping("/simplePojo")
    public String simplePojo(User user){
        System.out.println(user);
        return "OK";
    }
    //实体参数：复杂实体对象
    @RequestMapping("/complexPojo")
    public String complexPojo(User user){
        System.out.println(user);
        return "OK";
    }

    /*********************************************************数组集合参数**********************************************************/
    //数组参数
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] name){
        for (String s : name) {
            System.out.println(s);
        }
        System.out.println(Arrays.toString(name));
        return "OK";
    }
    //集合参数
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> name){
        System.out.println(name);
        return "OK";
    }

    /*********************************************************日期参数**********************************************************/
    @RequestMapping("/dateParam")
    public LocalDateTime dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime updateTime){
        return updateTime;
    }

    /*********************************************************JSON参数**********************************************************/
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user){//@RequestBody注解：将JSON数据映射到形参的实体类对象中（JSON中的key和实体类中的属性名保持一致）
        System.out.println(user);
        System.out.println(user.getAddress().getProvince());
        return "OK";
    }

    /*********************************************************路径参数**********************************************************/
    @GetMapping("/pathParam/{id}")
    public String pathParam(@PathVariable("id") int id){
        return "OK";
    }

    @RequestMapping("/path/{id}/{name}")
    public String pathParam2(@PathVariable Integer id, @PathVariable String name){
        System.out.println(id+ " : " +name);
        return "OK";
    }
}
